package br.edu.ufcg.p1.barzin.async;

import static br.edu.ufcg.p1.barzin.util.PrefManConstants.TABLET_ID;
import android.os.AsyncTask;
import br.edu.ufcg.p1.barzin.App;
import br.edu.ufcg.p1.barzin.event.EventBus;
import br.edu.ufcg.p1.barzin.event.MsgsRecuperadasEvento;
import br.edu.ufcg.p1.barzin.model.ListaMensagens;
import br.edu.ufcg.p1.barzin.util.HTTPCalls;

public class RecuperaMsgsTask extends AsyncTask<Integer, Integer, Integer> {

	@Override
	protected Integer doInBackground(Integer... id) {
		Integer idTablet = App.environment().getSharedPreferences()
				.getInt(TABLET_ID, -1);

		ListaMensagens total = HTTPCalls.recuperaNovasMsgs(id[0], idTablet);
		if (total != null) {
			App.environment().getDao().salvaMensagens(total.getLista());
			return id[0];
		}
		return null;
	}

	@Override
	protected void onPostExecute(Integer result) {
		if (result != null) {
			EventBus.getInstance().publica(new MsgsRecuperadasEvento(result));
		}
	}

}
